package com.wlstock.funi.ui;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.TableRow;
import android.widget.TextView;

import com.wlstock.funi.R;
import com.wlstock.funi.adapter.WenGuScrollAdapter;
import com.wlstock.funi.dao.MyAccountStockDao;
import com.wlstock.funi.dao.OperateDao;
import com.wlstock.funi.dao.PositionDao;
import com.wlstock.funi.dao.SelfFundDao;
import com.wlstock.funi.dao.SelfFundHisDao;
import com.wlstock.funi.dao.SelfFundSetDao;
import com.wlstock.funi.dao.StrategyDao;
import com.wlstock.funi.dao.StrategyFundSelDao;
import com.wlstock.funi.dao.TradeHisDao;
import com.wlstock.funi.model.BaseModel;
import com.wlstock.funi.model.MyAccountStocks;
import com.wlstock.funi.model.OperateModel;
import com.wlstock.funi.model.PositionModel;
import com.wlstock.funi.model.SelfFundHisModel;
import com.wlstock.funi.model.SelfFundModel;
import com.wlstock.funi.model.SelfFundSetModel;
import com.wlstock.funi.model.StrategyFundModel;
import com.wlstock.funi.model.StrategyModel;
import com.wlstock.funi.model.TradeHisModel;
import com.wlstock.funi.utils.StockUtils;
import com.wlstock.funi.widget.DialogContentView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class LocalDBActivity extends ScrollTableActivity{


	private String[] titles;
	private String actTitlle = "";
	private List<? extends BaseModel> curModels;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setTableVisible(true);
		layout_search_time.setVisibility(View.GONE);
		layout_stategy_time.setVisibility(View.GONE);
	}
	
	@Override
	public List<Map<String, Object>> getMenuList() {
		ArrayList<Map<String, Object>> menus = new ArrayList<Map<String,Object>>();
		Map<String, Object> item1 = new HashMap<String, Object>();
		item1 = new HashMap<String, Object>();
		item1.put("name", getResources().getString(R.string.pop_title_dao_position));
		menus.add(item1);

		item1 = new HashMap<String, Object>();
		item1.put("name", "持股表");
		menus.add(item1);

		item1 = new HashMap<String, Object>();
		item1.put("name", getResources().getString(R.string.pop_title_dao_wanglong_trade));
		menus.add(item1);
		
		item1 = new HashMap<String, Object>();
		item1.put("name", getResources().getString(R.string.pop_title_dao_selffund));
		menus.add(item1);

		item1 = new HashMap<String, Object>();
		item1.put("name", getResources().getString(R.string.pop_title_dao_selffund_set));
		menus.add(item1);
		
		item1 = new HashMap<String, Object>();
		item1.put("name", getResources().getString(R.string.pop_title_dao_ai_trade));
		menus.add(item1);

		item1 = new HashMap<String, Object>();
		item1.put("name", getResources().getString(R.string.pop_title_dao_strategy_fund_models));
		menus.add(item1);

		item1 = new HashMap<String, Object>();
		item1.put("name", "策略表");
		menus.add(item1);

		item1 = new HashMap<String, Object>();
		item1.put("name", "监测K线历史");
		menus.add(item1);

		return menus;
	}
	
	@Override
	public void onViewSelectListener(String titleTxt) {
		if (titleTxt.endsWith(getResources().getString(R.string.pop_title_dao_position))) {
			List<PositionModel> positionModels = PositionDao.getInstance(this).queryAll();
			configTable(getTableHash(positionModels));
		}else if (titleTxt.endsWith(getResources().getString(R.string.pop_title_dao_wanglong_trade))) {
			List<OperateModel> opModels = OperateDao.getInstance(this).queryAll();
			configTable(getTableHash(opModels));
		}else if (titleTxt.endsWith(getResources().getString(R.string.pop_title_dao_selffund))) {
			List<SelfFundModel> sfModels = SelfFundDao.getInstance(this).queryAllSelfFundModes();
			configTable(getTableHash(sfModels));
		}else if (titleTxt.endsWith(getResources().getString(R.string.pop_title_dao_selffund_set))) {
			List<SelfFundSetModel> sfSetModels = SelfFundSetDao.getInstance(this).queryAll();
			configTable(getTableHash(sfSetModels));
		}else if (titleTxt.endsWith(getResources().getString(R.string.pop_title_dao_ai_trade))) {
			List<TradeHisModel> tradeHisModels = TradeHisDao.getInstance(this).queryHisDatasOrderbyDate();
			configTable(getTableHash(tradeHisModels));
		}else if (titleTxt.endsWith(getResources().getString(R.string.pop_title_dao_strategy_fund_models))){
			List<StrategyFundModel> strategyFundModels = StrategyFundSelDao.getInstance(this).queryAll();
			configTable(getTableHash(strategyFundModels));
		}else if (titleTxt.endsWith("持股表")){
            List<MyAccountStocks> myAccountStockses = MyAccountStockDao.getInstance(this).queryAllStocks();
            configTable(getTableHash(myAccountStockses));
        }else if(titleTxt.endsWith("策略表")){
			List<StrategyModel> strategyModels = StrategyDao.getInstance(this).queryAll();
			configTable(getTableHash(strategyModels));
		}else if (titleTxt.endsWith("监测K线历史")){
			List<SelfFundHisModel> hisModels = SelfFundHisDao.getInstance(this).queryAll();
			configTable(getTableHash(hisModels));
		}
	}



	private void configTable(HashMap<String, Object> result) {
		if (result!=null) {
			String[] titles = (String[]) result.get(StockUtils.KEY_TITLE);
			ArrayList<ArrayList<String>> results = (ArrayList<ArrayList<String>>) result.get(StockUtils.KEY_RESULT);

			if (results!=null) {
				tv_search_count.setText("共"+result.get(StockUtils.KEY_COUNT)+"条");
			}

			wenGuScrollAdapter = null;
			wenGuScrollAdapter = new WenGuScrollAdapter(titles, results);
			wenGuScrollAdapter.onRowClickListener = this;
			scroll_table.setAdapter(wenGuScrollAdapter);
			scroll_table.refreshTable();
		}else {
			tv_search_count.setText("此结果无数据返回");

		}
	}

	private HashMap<String, Object> getTableHash(List<? extends BaseModel> models) {
		this.curModels = models;
		if (models != null && models.size()>0) {
			actTitlle = models.get(0).getClassName();
			setTitle(actTitlle);

			HashMap<String, Object> tableDataHash = new HashMap<String, Object>();
			titles = models.get(0).getFieldNames();
			tableDataHash.put(StockUtils.KEY_TITLE, titles);
			ArrayList<ArrayList<String>> result = new ArrayList<ArrayList<String>>();
			for (int i = 0; i < models.size(); i++) {
				result.add(models.get(i).getFiledValues());
			}
			tableDataHash.put(StockUtils.KEY_RESULT, result);
			tableDataHash.put(StockUtils.KEY_COUNT, models.size());
			return tableDataHash;
		}
		return null;
	}

	@Override
	public void rowClick(TableRow row, int position) {
		final BaseModel baseModel = curModels.get(position);

		int count = row.getChildCount();
		if (titles!=null && titles.length == count){
			HashMap<String, String> map = new HashMap<>();
			for (int i = 0; i < count; i++) {
				View cView = row.getChildAt(i);
				if (cView instanceof TextView) {
					String rowTV = ((TextView)cView).getText().toString();
					map.put(titles[i], rowTV);
				}
			}
			View v = DialogContentView.getInstance().listView(this, map,true);
			new AlertDialog.Builder(this).setTitle("编辑").setView(v).setNegativeButton("取消",null).setPositiveButton("确认", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
//					try {
//						Dao dao = DataBaseHelper.getHelper(LocalDBActivity.this).getDao(Class.forName(actTitlle));
//						baseModel.updateFiled()
//					} catch (SQLException e) {
//						e.printStackTrace();
//					} catch (ClassNotFoundException e) {
//						e.printStackTrace();
//					}
				}
			}).setNeutralButton("删除", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {

				}
			}).show();
		}

	}
}
